package Assignment09_AddingPolynomials;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Assignment09_AddingPolynomials {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		String str1 = input.nextLine();
		String str2 = input.nextLine();

		// System.out.println("str1::" + str1);
		// System.out.println("str2::" + str2);

		Scanner s1 = new Scanner(str1);
		ArrayList<Term> p1 = printNextInt(s1); // First arraylist getting
												// populated

		// System.out.println(" This is a separator");

		Scanner s2 = new Scanner(str2);
		ArrayList<Term> p2 = printNextInt(s2); // Second arraylist getting
												// populated

		resultPolyAdd(p1, p2);

	}

	private static ArrayList<Term> printNextInt(Scanner s) {

		ArrayList<Term> polynomial = new ArrayList<Term>();

		while (s.hasNextInt()) {

			int I1 = s.nextInt();
			int I2 = s.nextInt();
			// System.out.println("before" + I1 + "" + I2);
			if (!(I1 == -1 && I2 == -1)) {

				// System.out.println("after" + I1 + "" + I2); // the number
				// test
				Term p = new Term(I1, I2);
				polynomial.add(p);
			}

		}// while loop ends
			// System.out.println(polynomial);
		return polynomial;
	}

	private static void resultPolyAdd(ArrayList<Term> p1, ArrayList<Term> p2) {

		// ArrayList<Term> polynomial = new ArrayList<Term>();
		ArrayList<Term> poly_part1 = new ArrayList<Term>();

		// System.out.println(p1.size()); // 3
		// System.out.println(p2.size()); // 4

		for (int i = 0; i < p1.size(); i++) {
			for (int j = 0; j < p2.size(); j++) {

				if (p1.get(i).degree == p2.get(j).degree) {

					// maintain the degree and net the coeff
					Term t1 = new Term(
							(p1.get(i).co_efficient + p2.get(j).co_efficient),
							p1.get(i).degree);

					// add the object to the Arraylist
					poly_part1.add(t1);

				}

			}
		}
		// System.out.println(poly_part1 + "match");
		createFinalArray(p1, p2, poly_part1);
	}

	private static void createFinalArray(ArrayList<Term> p1,
			ArrayList<Term> p2, ArrayList<Term> poly_part1) {

		ArrayList<Term> poly_final = new ArrayList<Term>();

		// System.out.println("p1" + p1);
		// System.out.println("p2" + p2);
		// System.out.println("poly_part1" + poly_part1);

		for (int i = 0; i < p1.size(); i++) {
			int j = 0;

			for (; j < poly_part1.size(); j++) {

				if (p1.get(i).degree == poly_part1.get(j).degree) {

					break;

				}

			}
			if (j == poly_part1.size()) {

				Term t = new Term(p1.get(i).co_efficient, p1.get(i).degree);

				// add the object to the Arraylist
				poly_final.add(t);

			}

		} // nested for loop ends

		// second nested for loop starts
		for (int i = 0; i < p2.size(); i++) {
			int j = 0;

			for (; j < poly_part1.size(); j++) {

				if (p2.get(i).degree == poly_part1.get(j).degree) {

					break;

				}

			}
			if (j == poly_part1.size()) {

				Term t = new Term(p2.get(i).co_efficient, p2.get(i).degree);

				// add the object to the Arraylist
				poly_final.add(t);

			}

		}
		// System.out.println("poly_final(before addition)" + poly_final);

		for (int i = 0; i < poly_part1.size(); i++) {

			if (poly_part1.get(i).co_efficient != 0) {
				poly_final.add(poly_part1.get(i));
			}

		}

		// System.out.println("poly_final(after addition--before sort)"
		// + poly_final);
		sortedPolynomials(poly_final);

		System.out.println(poly_final.toString().replace(",", "")
				.replace("[", "").replace("]", ""));

	}

	private static void sortedPolynomials(ArrayList<Term> poly_final) {

		Collections.sort(poly_final, new Comparator<Term>() {
			@Override
			public int compare(Term t1, Term t2) {

				return t2.degree - t1.degree;
			}

		});

	}

}
